<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <button id="btn-set" >设置search</button>
    <button id="btn-get" >获取search</button>
    <script>
        var btnSet = document.getElementById('btn-set');
        var btnGet = document.getElementById('btn-get');
        var search = '?id=1&search=ok';

        btnSet.onclick = function () {
            location.search = search;
        }

        btnGet.onclick = function () {
            console.log(getValue(location.search, 'id'));
        }
        // ?id=1&search=ok

        function getValue(search, key) {
            //1 找出key第一次出现的位置
            var start = search.indexOf(key);
            if (start === -1) {
                return;
            }else {
                //2 找出键值对，结束的位置
                var end = search.indexOf('&', start);
                if (end === -1) {
                    //这是最后一个键值对
                    end = search.length;
                }
            }

            //3 将键值对提取出来
            var str = search.slice(start, end);
            var arr = str.split('=');
            return arr[1];
        }

    </script>
</body>
</html>